package cn.ztr.ymcc.consumer;

import cn.ztr.ymcc.domain.MessageSms;
import cn.ztr.ymcc.domain.MessageStation;
import cn.ztr.ymcc.dto.MessageDTO;
import cn.ztr.ymcc.service.IMessageSmsService;
import cn.ztr.ymcc.service.IMessageStationService;
import com.alibaba.fastjson.JSON;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "ztr_station",
        topic = "ztr_topic",
        selectorExpression = "station"
)
public class StationConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMessageStationService stationService;

    @Override
    public void onMessage(MessageExt messageExt) {
        String message = new String(messageExt.getBody(), CharsetUtil.UTF_8);
        MessageDTO messageDTO = JSON.parseObject(message, MessageDTO.class);
        MessageStation messageStation = new MessageStation();
        log.debug(String.valueOf(messageDTO));
        for (MessageDTO.Station station : messageDTO.getStations()) {
            BeanUtils.copyProperties(station,messageStation);
            BeanUtils.copyProperties(messageDTO,messageStation);
            messageStation.setSendTime(new Date());
            stationService.save(messageStation);
        }
    }
}
